﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace MINERP.DAL.Statistics
{
    public class BackOrderDAL:DALBase
    {
        #region 获取指定商户所有冲印的产品集合
        /// <summary>
        /// 获取指定商户所有冲印的产品集合
        /// </summary>
        /// <returns></returns>
        public List<Tb_Product> GetProduct(int shopId)
        {
            try
            {
                //冲印类，根据实际情况改变
                List<Tb_Product> ListProduct = DataEntites.Tb_Product.Where(p => p.Tb_PorductCategory.ProductCategoryId == 2).ToList<Tb_Product>();
                List<Shop_Product_Price> ListSPP = DataEntites.Shop_Product_Price.Where(p => p.ShopId == shopId).ToList<Shop_Product_Price>();
                List<int> ListProductId = new List<int>();
                foreach (Shop_Product_Price spp in ListSPP)
                {
                    if (!ListProductId.Contains(spp.ProductId))
                    {
                        ListProductId.Add(spp.ProductId);
                    }
                }
                List<Tb_Product> ListFilterProduct = new List<Tb_Product>();
                foreach (Tb_Product product in ListProduct)
                {
                    foreach (int productId in ListProductId)
                    {
                        if (product.ProuctId == productId)
                        {
                            ListFilterProduct.Add(product);
                            break;
                        }
                    }
                }
                return ListFilterProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取由商户ID与产品Id确定的纸型、纸面、面积单价
        /// <summary>
        /// 获取由商户ID与产品Id确定的纸型、纸面、面积单价
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public List<string> GetPaperTypeAndPaperFace(int shopId, int productId)
        {
            try
            {
                Shop_Product_Price spp = DataEntites.Shop_Product_Price.Include("paperType").Include("paperFace").Where(p => p.ShopId == shopId && p.ProductId == productId).First();
                //string paperType = spp.paperType.Name;
                //string paperFace = spp.paperFace.Name;
                //string AreaPrice = spp.AreaPrice.ToString();
                string paperType = "a";
                string paperFace = "d";
                string AreaPrice = "c";
                List<string> paperTypeFace = new List<string>();
                paperTypeFace.Add(paperType);
                paperTypeFace.Add(paperFace);
                paperTypeFace.Add(AreaPrice);
                return paperTypeFace;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 开退单
        /// <summary>
        /// 开退单
        /// </summary>
        /// <param name="billId"></param>
        /// <param name="shopId"></param>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <param name="ctmName"></param>
        /// <param name="pName"></param>
        /// <param name="completedStatus"></param>
        /// <param name="remark"></param>
        public void BackOrder(int billId,int productId, int shopId, double height, double width,double unitPrice, string ctmName,int pageNum, string pName, int completedStatus, string remark)
        {
            try
            {
                TransactionOptions option = new TransactionOptions();
                option.Timeout = new TimeSpan(0, 0, 600);
                option.IsolationLevel = IsolationLevel.ReadCommitted;
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, option))
                {
                    //根据billId得到该账单对应的其中一个订单
                    Tb_OrderBag order = GetOrderByBillId(billId);
                    Tb_Product product = GetProductByProductId(productId);
                    string m = string.Format("{0:0.##}", (height * 2.54 / 100) * (width * 2.54 / 100) * pageNum);
                    double money = Convert.ToDouble(m);
                    double area=height*width;
                    //1 插入orderInfo
                    int orderInfoId = InsertOrderInfo(order, product, "2000000000", pName, height, width, pageNum, ctmName, money, remark, completedStatus);
                    string orderCode ="2"+ string.Format("{0:d9}", orderInfoId);        //产品编号
                    UpdateOrderInfo(orderInfoId, orderCode);                            //修改产品编号
                    //2 插入Bill_Detail
                    SHOP_Bill bill=GetBillByBillId(billId);
                    Bill_Detail bd = InsertBillDetail(bill, orderCode, pName);

                    //3 插入Bill_Detail_Detail
                    InsertBillDetailDetail(bd, pageNum, height, width, area, money, unitPrice);

                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 根据billId获取其对应的其中一个订单
        /// <summary>
        /// 根据billId获取其对应的其中一个订单
        /// </summary>
        /// <param name="billId"></param>
        /// <returns></returns>
        public Tb_OrderBag GetOrderByBillId(int billId)
        {
            try
            {
                Bill_Detail bd = DataEntites.Bill_Detail.Where(p => p.SHOP_Bill.BillId == billId).First();
                string orderInfoCode = bd.OrderInfoCode;
                orderInfo info = DataEntites.orderInfo.Where(p => p.OrderProductCode == orderInfoCode).First();
                if (!info.Tb_OrderBagReference.IsLoaded)
                {
                    info.Tb_OrderBagReference.Load();
                }
                Tb_OrderBag order = info.Tb_OrderBag;
                return order;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 插入新产品
        /// <summary>
        /// 插入新产品
        /// </summary>
        public int InsertOrderInfo(Tb_OrderBag order,Tb_Product product,string productCode,string pName,double height,double width,int pageNum,string ctmName,double money,string remark,int completedStatus)
        {
            try
            {
                orderInfo info = new orderInfo();
                info.CompleteTime = DateTime.Now;
                info.CtmName = ctmName;
                info.Height = height;
                info.Width = width;
                info.IsEdit = false;
                info.OrderPath = " ";
                info.OrderProductCode = productCode;
                info.OrderProductName = pName;
                info.OrilOrderPath = " ";
                info.PageNum = pageNum;
                info.PrePriceCount = -Convert.ToDecimal(money);
                info.productNum = 1;
                info.Remark = remark;
                info.status = completedStatus;
                info.Tb_OrderBag = order;
                info.Tb_Product = product;
                DataEntites.SaveChanges();
                return Convert.ToInt32(info.EntityKey.EntityKeyValues[0].Value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 修改新增产品的编号
        /// <summary>
        /// 修改新增产品的编号
        /// </summary>
        /// <param name="orderInfoId"></param>
        /// <param name="infoCode"></param>
        public void UpdateOrderInfo(int orderInfoId,string infoCode)
        {
            try
            {
                orderInfo info = DataEntites.orderInfo.Where(p => p.OrderProductId == orderInfoId).First();
                info.OrderProductCode = infoCode;
                DataEntites.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 插入Bill_Detail
        /// <summary>
        /// 插入Bill_Detail
        /// </summary>
        /// <param name="bill"></param>
        /// <param name="orderInfoCode"></param>
        /// <param name="productName"></param>
        public Bill_Detail InsertBillDetail(SHOP_Bill bill,string orderInfoCode,string productName)
        {
            try
            {
                Bill_Detail bd = new Bill_Detail();
                bd.OrderInfoCode = orderInfoCode;
                bd.OrderProductName = productName;
                bd.SHOP_Bill = bill;
                DataEntites.SaveChanges();
                return bd;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 插入Bill_Detail_Detail
        /// <summary>
        /// 插入Bill_Detail_Detail
        /// </summary>
        public void InsertBillDetailDetail(Bill_Detail bd,int pageNum,double height,double width,double area,double money,double unitPrice)
        {
            try
            {
                Bill_Detail_Detail bdd = new Bill_Detail_Detail();
                bdd.Area = area;
                bdd.Bill_Detail = bd;
                bdd.Count = -money;
                bdd.DetailName = "退单";
                bdd.Num = pageNum;
                string inchStr = width.ToString() + "*" + height.ToString() + "in";
                string cmStr = (width * 2.54).ToString() + "*" + (height * 2.54).ToString() + "cm";
                bdd.Spec = inchStr + "(" + cmStr + ")";
                bdd.UnitPrice = unitPrice;
                DataEntites.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 根据productId获取产品
        /// <summary>
        /// 根据productId获取产品
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public Tb_Product GetProductByProductId(int productId)
        {
            try
            {
                Tb_Product product = DataEntites.Tb_Product.Where(p => p.ProuctId == productId).First();
                return product;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 根据BillId获取Bill
        /// <summary>
        /// 根据BillId获取Bill
        /// </summary>
        /// <param name="billId"></param>
        /// <returns></returns>
        public SHOP_Bill GetBillByBillId(int billId)
        {
            try
            {
                SHOP_Bill bill = DataEntites.SHOP_Bill.Where(p => p.BillId == billId).First();
                return bill;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

    }
}
